package com.zzzzzz.oms.legs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.StringTokenizer;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.zzzzzz.oms.tempShipment.TempShipmentDao;
import com.zzzzzz.plugins.jdbctemplate.BaseDao;
import com.zzzzzz.plugins.jdbctemplate.Finder;
import com.zzzzzz.plugins.jdbctemplate.DaoUtils;
import com.zzzzzz.utils.page.Page;
import com.zzzzzz.plugins.shiro.I;


/**
 * @author hing
 * @version 1.0.0
 */
@Repository
public class LegsDao {
	
	@Resource
	private BaseDao baseDao;
	
	@Resource
	private TempShipmentDao tempShipmentDao;
	
	// SQL
	private final static String sql_add = "insert into t_legs(orderId, orderCd, legs_no, st, platformId, vocationType, clientId, shipment_method, formId, toId, ftranslocationId, ttranslocationId, formName, formlikeName, formPhone, formAddr, toName, tolikeName, toPhone, toAddr, tempshipmentId, quantity, weight, volume, palletsum, expense, component, componentMark, ordertime, billtime, planltime, planatime, addDt, addBy) values(:orderId, :orderCd, :legs_no, 'shipment', :platformId, :vocationType, :clientId, :shipment_method, :formId, :toId, :ftranslocationId, :ttranslocationId, :formName, :formlikeName, :formPhone, :formAddr, :toName, :tolikeName, :toPhone, :toAddr, :tempshipmentId, :quantity, :weight, :volume, :palletsum, :expense, :component, :componentMark, :ordertime, :billtime, :planltime,:planatime, :addDt, :addBy)";
	private final static String sql_updTempshipmentByIds = "update t_legs set tempshipmentId=:tempshipmentId,updDt = :updDt, updBy = :updBy where id in (:ids)";
	private final static String sql_updStById = "update t_legs set tempshipmentId=:tempshipmentId,updDt = :updDt, updBy = :updBy where tempshipmentId in (:ids)";
	private final static String sql_updByWaveId = "update t_legs set tempshipmentId=:tempshipmentId,updDt = :updDt, updBy = :updBy where waveId =:waveId and tempshipmentId=:tempshipmentId1";
	private final static String sql_updWaveIdById = "update t_legs set waveId=:waveId,updDt = :updDt, updBy = :updBy where id=:id";
	private final static String sql_upd = "update t_legs set orderId=:orderId, orderCd=:orderCd, legs_no=:legs_no, st=:st, platformId=:platformId,vocationType=:vocationType, clientId=:clientId, constractorId=:constractorId, shipment_method=:shipment_method, formId=:formId, toId=:toId,ftranslocationId=:ftranslocationId,ttranslocationId=:ttranslocationId, formName=:formName, formlikeName=:formlikeName, formPhone=:formPhone, formAddr=:formAddr, toName=:toName, tolikeName=:tolikeName, toPhone=:toPhone, toAddr=:toAddr, shipmentId=:shipmentId, shipmentType=:shipmentType, quantity=:quantity, weight=:weight, volume=:volume, palletsum=:palletsum, expense=:expense,waveId=:waveId,component=:component, bag=:bag, ordertime=:ordertime, billtime=:billtime, planltime=:planltime, planatime=:planatime,leavetime=:leavetime, arrivetime=:arrivetime, updDt=:updDt, updBy=:updBy where id = :id";
	private final static String sql_findById = "select id, orderId, orderCd, legs_no, st, platformId, vocationType, clientId, constractorId,shipment_method, formId, toId,ftranslocationId,ttranslocationId, formName, formlikeName, formPhone, formAddr, toName, tolikeName, toPhone, toAddr, shipmentId,tempshipmentId, shipmentType, quantity, weight, volume, palletsum, expense,component, bag, pcTime,rkTime,ckTime,ordertime, billtime, planltime,planatime, leavetime, arrivetime, addDt, addBy, updDt, updBy from t_legs where id = :id";
	private final static String sql_findByIds = "select id,orderId, orderCd, legs_no, st, vocationType, tempshipmentId, quantity, weight, volume, palletsum, expense from t_legs where  orderId in (:ids)";
	private final static String sql_updByTempShipmentId = "update t_legs set constractorId=:constractorId,shipment_method=:shipment_method,st=:st where tempshipmentId=:tempshipmentId";
	private final static String sql_updShipmentId = "update t_legs set shipmentId=:shipmentId,updDt = :updDt, updBy = :updBy where tempshipmentId=:tempshipmentId";
	private final static String sql_findByTempshipmentId = "select ftranslocationId, ttranslocationId from t_legs where tempshipmentId=:tempshipmentId";
	private final static String sql_updByIds = "update t_legs set shipmentId=NULL,tempshipmentId=:tempshipmentId,constractorId=NULL,st='shipment',pcTime=NULL,rkTime=NULL,ckTime=NULL,leavetime=NULL,arrivetime=NULL,updDt = :updDt, updBy = :updBy where id in (:ids)";
	private final static String sql_updByIds1 = "update t_legs set shipmentId=:shipmentId,tempshipmentId=NULL,constractorId=:constractorId,shipment_method=:shipment_method,st='shipmentEd',updDt = :updDt, updBy = :updBy where id in (:ids)";
	private final static String sql_updByWaveIds = "update t_legs set waveId=:waveId,updDt = :updDt, updBy = :updBy where id in (:ids)";
	private final static String sql_delByIds = "delete from t_legs where orderId in (:orderIds)";
	private final static String sql_delByIds1 = "delete from t_legs where id =:id";
	private final static String sql_updStByIds = "update t_legs set st=:st,updDt = :updDt, updBy = :updBy where id in (:ids)";
	private final static String sql_findGroupByWaveIds = "select sum(l.quantity) as quantity,sum(l.weight) as weight,sum(l.volume) as volume,sum(l.palletsum) as palletsum,(select count(*) from (select count(toAddr),tempshipmentId from t_legs group by tempshipmentId,toAddr having tempshipmentId=:tempshipmentId and count(toAddr) !=0) as l) as count,count(l.id) as points,l.vocationType,l.waveId ,w.cd as waveCd ,l.tempshipmentId from t_legs l left join t_wave w on w.id=l.waveId group by l.waveId,l.tempshipmentId having l.waveId is not null and l.tempshipmentId=:tempshipmentId";
	private final static String sql_findtempIds = "select sum(quantity) as quantity,sum(weight) as weight,sum(volume) as volume,sum(palletsum) as palletsum,(select count(*) from (select count(toAddr),tempshipmentId from t_legs group by tempshipmentId,toAddr having tempshipmentId=:tempshipmentId and count(toAddr) !=0) as l) as count,count(id) as points  from t_legs where tempshipmentId=:tempshipmentId";
	private final static String sql_findshipmentIds = "select sum(quantity) as quantity,sum(weight) as weight,sum(volume) as volume,sum(palletsum) as palletsum,(select count(*) from (select count(toAddr),shipmentId from t_legs group by shipmentId,toAddr having shipmentId=:shipmentId and count(toAddr) !=0) as l) as count,count(id) as points  from t_legs where shipmentId=:shipmentId";
	private final static String sql_findGrouptempIds = "select tempshipmentId,sum(quantity) as quantity,sum(weight) as weight,sum(volume) as volume,sum(palletsum) as palletsum,count(id) as points from t_legs group by tempshipmentId having tempshipmentId in(:ids)";
	private final static String sql_findCountByTempId = "select count(*) as count from (select count(toAddr),tempshipmentId from t_legs group by tempshipmentId,toAddr having tempshipmentId=:tempshipmentId and count(toAddr) !=0) as l";
	private final static String sql_findByCd = "select id,orderId, orderCd, legs_no, st, platformId,vocationType, clientId, constractorId,shipment_method, formId, toId,ftranslocationId,ttranslocationId,formName, formlikeName, formPhone, formAddr, toName, tolikeName, toPhone, toAddr, shipmentId, tempshipmentId, shipmentType, quantity, weight, volume, palletsum, expense,component, bag,pcTime,rkTime,ckTime, ordertime, billtime, planltime,planatime, leavetime, arrivetime, addDt, addBy,updDt,updBy from t_legs where legs_no=:legs_no";
	private final static String sql_findBycomponentMark = "select legs_no from t_legs where componentMark=:id";
	private final static String sql_updMustDtByOrderId = "update t_order_head t set mustDt = :mustDt where 1=(select count(id)=count(arrivetime) from t_legs l  where l.orderId = :id) and t.id = :orderId";
	
	// 增加，主要用于订单生效之后生成分段订单
	public Long add(Legs legs){
		return baseDao.updateGetLongKey(sql_add, legs);
	}
	
	// 主要是用于创建配载的时候，改变分段订单的tempshipmentId
	public int updTempshipmenByIds(Long tempshipmentId, List<Long> ids, I i){
		Finder finder = new Finder(sql_updTempshipmentByIds).setParam("tempshipmentId", tempshipmentId).setParam("ids", ids)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	
	// 主要用于订单做运抵确认的时候生成订单的应回单时间
	public int updMustDtByOrderId(Date mustDt, Long orderId){
		Finder finder = new Finder(sql_updMustDtByOrderId).setParam("mustDt", mustDt).setParam("id", orderId).setParam("orderId", orderId);
		return baseDao.update(finder);
	}
	
	// 删除分段订单的拆单
	public int delByIds1(Long id) {
		Finder finder = new Finder(sql_delByIds1).setParam("id", id);
		return baseDao.update(finder);
	}
	
	// 主要用于订单的时效
	public int delByIds(List<Long> ids) {
		Finder finder = new Finder(sql_delByIds).setParam("orderIds", ids);
		return baseDao.update(finder);
	}
	
	//生效订单触发
	public List<Legs> findGrouptempIds(List<Long> ids) {
		Finder finder = new Finder(sql_findGrouptempIds).setParam("ids", ids);
		return baseDao.findList(finder, Legs.class);
	}
	
	//生效订单触发
	public int findCountByTempId(Long tempShipmentId) {
		Finder finder = new Finder(sql_findCountByTempId).setParam("tempshipmentId", tempShipmentId);
		Legs legs=baseDao.findOne(finder, Legs.class);
		return legs.getCount();
	}
	
	public int updStByIds(Object ids,Object st,I i){
		Finder finder = new Finder(sql_updStByIds).setParam("ids", ids).setParam("st", st)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	
	public int updByIds(List<Long> ids,Long tempShipmentId,I i){
		Finder finder = new Finder(sql_updByIds).setParam("ids", ids).setParam("tempshipmentId",tempShipmentId)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	
	public int updWaveIdById(Long waveId,Long id,I i){
		Finder finder = new Finder(sql_updWaveIdById).setParam("waveId", waveId).setParam("id",id)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	
	public int updStByWaveIds(List<Long> ids,Long waveId,I i){
		Finder finder = new Finder(sql_updByWaveIds).setParam("ids", ids).setParam("waveId",waveId)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	
	public int updByIds1(List<Long> ids,Long shipmentId,Long constractorId,String shipment_method,I i){
		Finder finder = new Finder(sql_updByIds1).setParam("ids", ids).setParam("shipmentId",shipmentId)
		.setParam("constractorId", constractorId).setParam("shipment_method", shipment_method).setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	public int updStById(List<Long> ids,Long tempshipmentId,I i){
		Finder finder = new Finder(sql_updStById).setParam("ids", ids).setParam("tempshipmentId", tempshipmentId)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}	
	
	public int updByWaveId(Long waveId,Long tempshipmentId,Long tempshipmentId1,I i){
		Finder finder = new Finder(sql_updByWaveId).setParam("waveId", waveId).setParam("tempshipmentId", tempshipmentId).setParam("tempshipmentId1", tempshipmentId1)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}	
	
	public List<Legs> findByTempShipmentId(Long tempShipmentId){
		Finder finder = new Finder(sql_findByTempshipmentId).setParam("tempshipmentId", tempShipmentId);
		return baseDao.findList(finder,Legs.class);
	}
	
	// 主要是用于失效订单
	public List<Legs> findByIds(List<Long> ids){
		Finder finder = new Finder(sql_findByIds).setParam("ids", ids);
		return baseDao.findList(finder,Legs.class);
	}	
	
	public Legs findByCd(String cd){
		Finder finder = new Finder(sql_findByCd).setParam("legs_no", cd);
		return baseDao.findOne(finder, Legs.class);
	}
	
	public Legs findBycomponentMark(Long id){
		Finder finder = new Finder(sql_findBycomponentMark).setParam("id",id);
		return baseDao.findOne(finder, Legs.class);
	}
	
	//波次自动配载
	public List<Legs> findGroupByWaveIds(Long tempshipmentId){
		Finder finder = new Finder(sql_findGroupByWaveIds).setParam("tempshipmentId", tempshipmentId);
		return baseDao.findList(finder,Legs.class);
	}
	
	//计算未配载的数量
	public Legs findtempIds(Long tempShipmentId){
		Finder finder = new Finder(sql_findtempIds).setParam("tempshipmentId", tempShipmentId);
		return baseDao.findOne(finder, Legs.class);
	}
	
	//计算未配载的数量
	public Legs findshipmentIds(Long shipmentId){
		Finder finder = new Finder(sql_findshipmentIds).setParam("shipmentId", shipmentId);
		return baseDao.findOne(finder, Legs.class);
	}
	
	public int updByTempShipmentId(Long tempShipmentId,Long constractorId,String shipment_method,String st, I i){
		Finder finder = new Finder(sql_updByTempShipmentId).setParam("tempshipmentId", tempShipmentId).setParam("constractorId", constractorId)
		.setParam("shipment_method", shipment_method).setParam("st", st)		
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	
	public int updShipmentId(Long tempShipmentId,Long shipmentId, I i){
		Finder finder = new Finder(sql_updShipmentId).setParam("tempshipmentId", tempShipmentId).setParam("shipmentId", shipmentId)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	
	public int updById(Legs legs){
		return baseDao.update(sql_upd, legs);
	}
	
	public Legs findById(Long id){
		Finder finder = new Finder(sql_findById).setParam("id", id);
		return baseDao.findOne(finder, Legs.class);
	}	

	public int updTime(String time, String status, List<Long> ids, List<Long> shipmentIds, I i){
		Finder finder=new Finder("");
		finder.append("update t_legs set");
		if(status.equals("sendCar")){
			finder.append("pcTime = :pcTime").setParam("pcTime", time);
		}else if(status.equals("instorage")){
			finder.append("rkTime = :rkTime").setParam("rkTime", time);
		}else if(status.equals("outstorage")){
			finder.append("ckTime = :ckTime").setParam("ckTime", time);
		}else if(status.equals("onway")){
			finder.append("leavetime = :fyTime").setParam("fyTime", time);
		}else if(status.equals("arrive")){
			finder.append("arrivetime = :ydTime").setParam("ydTime", time);
		}
		if(ids.size() > 0){
			finder.append("where id in (:ids)").setParam("ids", ids);
		}
		if(shipmentIds.size() > 0){
			if(status.equals("onway") || status.equals("arrive")){
				finder.append(",st = :st").setParam("st", status);
			}
			finder.append("where shipmentId in (:shipmentIds)").setParam("shipmentIds", shipmentIds);
		}
		return baseDao.update(finder);
	}
	
	//分段订单跟踪
	public int zz(String table, String time, String status, String str,Long id){
		Finder finder=new Finder("");
		finder.append("update "+table+" t");
		if(status.equals("sendCar")){
			finder.append("set status='sendCar'");
			finder.append("where 1=(select count(id)=count(pcTime) from t_legs l  where l."+str+" = :id)").setParam("id", id);
		}else if(status.equals("instorage")){
			finder.append("set status='instorage'");
			finder.append("where 1=(select count(id)=count(rkTime) from t_legs l  where l."+str+" = :id)").setParam("id", id);
		}else if(status.equals("outstorage")){
			finder.append("set status='outstorage'");
			finder.append("where 1=(select count(id)=count(ckTime) from t_legs l  where l."+str+" = :id)").setParam("id", id);
		}else if(status.equals("onway")){
			finder.append("set status='onway'");
			if(table.equals("t_shipment")){
				finder.append(", leavetime=:leavetime").setParam("leavetime", time);
			}
			finder.append("where 1=(select count(id)=count(leavetime) from t_legs l  where l."+str+" = :id)").setParam("id", id);
		}else if(status.equals("arrive")){
			finder.append("set status='arrive'");
			if(table.equals("t_shipment")){
				finder.append(", arrivetime=:arrivetime").setParam("arrivetime", time);
			}
			finder.append("where 1=(select count(id)=count(arrivetime) from t_legs l  where l."+str+" = :id)").setParam("id", id);
		}
		finder.append("and t.id=:id").setParam("id", id);
		return baseDao.update(finder);
	}
		
	/**
	 * 动态查询
	 */
	public List<Legs> findListBy(Page page,Map<String, Object> ffMap,I i){
		Finder finder = new Finder("");
		finder.append(" select");
		finder.append("count(1) as tt");
		finder.append("from t_legs l inner join t_order_head o on o.id=l.orderId inner join sys_dict d on o.status=d.descr inner join sys_dict d1 on l.st=d1.descr"
				+ " inner join t_client c on l.clientId=c.id left join sys_dict d2 on l.shipment_method=d2.descr "
				+ " inner join t_trans_location ft on ft.id=l.ftranslocationId inner join t_trans_location tt on tt.id=l.ttranslocationId inner join sys_city sc on sc.id = tt.cityId "
				+ " left join t_shipment s on s.id=l.shipmentId left join t_vehicle v on v.id=s.vehicleId inner join sys_dict dv on dv.descr=l.vocationType left join t_wave w on w.id=l.waveId");
		finder.append(" where 1=1");
		finder.append("and l.platformId=:platformId").setParam("platformId", i.getPlatformId());
		if(DaoUtils.isNotNull(ffMap.get("st"))){
			finder.append(" and l.st = :st").setParam("st", ffMap.get("st"));
		}	
		if(DaoUtils.isNotNull(ffMap.get("tempshipmentId"))){	
			finder.append(" and l.tempshipmentId=:tempshipmentId").setParam("tempshipmentId", ffMap.get("tempshipmentId"));
		}
		if(DaoUtils.isNotNull(ffMap.get("shipmentId"))){	
			finder.append(" and l.shipmentId=:shipmentId").setParam("shipmentId", ffMap.get("shipmentId"));
		}
		if(DaoUtils.isNotNull(ffMap.get("orderCd"))){	
			finder.append(" and l.orderCd=:orderCd").setParam("orderCd", ffMap.get("orderCd"));
		}
		if(DaoUtils.isNotNull(ffMap.get("shipmentCd"))){	
			finder.append(" and s.cd=:shipmentCd").setParam("shipmentCd", ffMap.get("shipmentCd"));
		}
		if(DaoUtils.isNotNull(ffMap.get("clientId"))){	
			finder.append(" and l.clientId in (:clientId)").setParam("clientId", ffMap.get("clientId"));
		}
		if(DaoUtils.isNotNull(ffMap.get("status"))){	
			finder.append(" and o.status=:status").setParam("status", ffMap.get("status"));
		}
		if (DaoUtils.isNotNull(ffMap.get("vocation"))) {
			finder.append("and l.vocationType = :vocation").setParam("vocation",
					ffMap.get("vocation"));
		}
		if(DaoUtils.isNotNull(ffMap.get("planaDt1"))){
			finder.append("and date(l.planatime) >=date_format(:planaDt1,'%Y-%m-%d')").setParam("planaDt1", ffMap.get("planaDt1"));
		}
		if(DaoUtils.isNotNull(ffMap.get("planaDt2"))){ 
			finder.append("and date(l.planatime) <=date_format(:planaDt2,'%Y-%m-%d')").setParam("planaDt2", ffMap.get("planaDt2"));
		}
		if (DaoUtils.isNotNull(ffMap.get("orderCd1"))) {
			String str=ffMap.get("orderCd1").toString();
			List<String> list=new ArrayList<String>();
	        StringTokenizer st=new StringTokenizer(str,"\t\n\r\f"+" "+",");
	        while(st.hasMoreTokens()){
	        	list.add(st.nextToken());
	        }
			finder.append("and l.orderCd  in(:orderCd1)").setParam("orderCd1", list);
		}
		finder.append("order by l.id desc");
		if (page != null) {
			Integer total = baseDao.findInt(finder);
			page.setTotal(total);
			finder.limit(page.getStart(), page.getOffset());
		}
		String sql = finder.getSql().replace("count(1) as tt","l.*,o.status,d.val as statusName ,d1.val as stName,c.name as clientName,sc.name as cityName,"
				+ "d2.val as shipment_methodName,ft.name as ftranslocationName,tt.name as ttranslocationName,s.cd as shipmentCd,s.expense as shipmentExpese,"
				+ "v.vehicleNo as vehicleCd,dv.val as vocationName,s.driverName as driverName,w.name as waveName,o.account as account,o.rtime as rtime");
		finder.setSql(sql);
		List<Legs> list = baseDao.findList(finder, Legs.class);
				
		return list;
	}
	
	/**
	 * 主要用于配载
	 */
	public List<Legs> findListBy1(Page page,Map<String, Object> ffMap,I i){
		Finder finder = new Finder("");
		finder.append(" select");
		finder.append("count(1) as tt");
		finder.append("from t_legs l inner join t_client c on l.clientId=c.id inner join t_trans_location ft on ft.id=l.ftranslocationId"
				+ " inner join t_trans_location tt on tt.id=l.ttranslocationId  left join t_wave w on w.id=l.waveId inner join sys_city sc on sc.id = tt.cityId");
		finder.append(" where 1=1");
		finder.append("and l.platformId=:platformId").setParam("platformId", i.getPlatformId());
		if(DaoUtils.isNotNull(ffMap.get("tempshipmentId"))){	
			finder.append(" and l.tempshipmentId=:tempshipmentId").setParam("tempshipmentId", ffMap.get("tempshipmentId"));
		}
		if(DaoUtils.isNotNull(ffMap.get("shipmentIds"))){	
			finder.append(" and l.shipmentId in (:shipmentIds)").setParam("shipmentIds", ffMap.get("shipmentIds"));
		}
		if(DaoUtils.isNotNull(ffMap.get("orderCd"))){	
			finder.append(" and l.orderCd=:orderCd").setParam("orderCd", ffMap.get("orderCd"));
		}
		if(DaoUtils.isNotNull(ffMap.get("clientId"))){	
			finder.append(" and l.clientId in (:clientId)").setParam("clientId", ffMap.get("clientId"));
		}
		if(DaoUtils.isNotNull(ffMap.get("toAddr"))){
			finder.append(" and l.toAddr like :toAddr").setParam("toAddr", "%" + ffMap.get("toAddr") + "%");
		}
		if(DaoUtils.isNotNull(ffMap.get("cityId"))){	
			finder.append("and tt.cityId=:cityId").setParam("cityId", ffMap.get("cityId"));
		}
		if(DaoUtils.isNotNull(ffMap.get("planaDt1"))){
			finder.append("and date(l.planatime) >=date_format(:planaDt1,'%Y-%m-%d')").setParam("planaDt1", ffMap.get("planaDt1"));
		}
		if(DaoUtils.isNotNull(ffMap.get("planaDt2"))){ 
			finder.append("and date(l.planatime) <=date_format(:planaDt2,'%Y-%m-%d')").setParam("planaDt2", ffMap.get("planaDt2"));
		}
		if (DaoUtils.isNotNull(ffMap.get("orderCd1"))) {
			String str=ffMap.get("orderCd1").toString();
			List<String> list=new ArrayList<String>();
	        StringTokenizer st=new StringTokenizer(str,"\t\n\r\f"+" "+",");
	        while(st.hasMoreTokens()){
	        	list.add(st.nextToken());
	        }
			finder.append("and l.orderCd  in(:orderCd1)").setParam("orderCd1", list);
		}
		finder.append("order by l.id desc");
		if (page != null) {
			Integer total = baseDao.findInt(finder);
			page.setTotal(total);
			finder.limit(page.getStart(), page.getOffset());
		}
		String sql = finder.getSql().replace("count(1) as tt","l.id, l.legs_no, l.vocationType, l.formName, l.formAddr, l.toName, l.toAddr, l.quantity, l.weight, l.volume, l.palletsum, l.ordertime, l.billtime, l.planltime, l.planatime, l.tempshipmentId, c.name as clientName, ft.name as ftranslocationName, tt.name as ttranslocationName, sc.name as cityName");
		finder.setSql(sql);
		List<Legs> list = baseDao.findList(finder, Legs.class);
				
		return list;
	}
	
	/**
	 * 主要用于调度订单的跟踪
	 */
	public List<Legs> findListByShipmentIds(List<Long> shipmentIds,I i){
		Finder finder = new Finder("");
		finder.append(" select");
		finder.append("l.id, l.orderId, l.clientId, l.shipmentId, tt.name as ttranslocationName,sc.name as cityName ");
		finder.append("from t_legs l inner join  t_trans_location tt on tt.id=l.ttranslocationId inner join sys_city sc on sc.id = tt.cityId");
		finder.append(" where 1=1");
		finder.append("and l.platformId=:platformId").setParam("platformId", i.getPlatformId());	
		finder.append(" and l.shipmentId in (:shipmentIds)").setParam("shipmentIds", shipmentIds);
		List<Legs> list = baseDao.findList(finder, Legs.class);		
		return list;
	}
	
	/*
	 * 批量出入数据
	 */
	public void saveOrder(List<Legs> legs){
		baseDao.batchUpdate(sql_add, legs);
	}
}
